/*
 * get the items of each board
 */
package com.sysu.minsysu.zxcx;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;


//import org.htmlparser.util.NodeList;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import org.w3c.dom.Node;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.sysu.minsysu.zxcx.HtmlServices;
import com.sysu.R;
import com.sysu.minsysu.zxcx.NewsNetwork.BBSLinkNode;
import com.sysu.minsysu.zxcx.NewsNetwork.LinkNode;

public class ShowBBSEntry extends Activity{
	LinkNode mLinkNode;
	private static final String TAG = "ShowBBSEntry";
	ArrayList<BBSLinkNode> bbsLinkNodes;
	static final String BBSSysuSite = "http://bbs.sysu.edu.cn";
	private static HtmlServices service;
	@Override
	public void onCreate( Bundle savedInstance ){
		super.onCreate(savedInstance);
		setContentView(R.layout.newsmain);
		LinearLayout mLayout = (LinearLayout)findViewById(R.id.layout);	
		mLinkNode = (LinkNode)getIntent().getSerializableExtra("linknode");
				
		bbsLinkNodes = getEntry(mLinkNode.link);
		Log.d(TAG, BBSSysuSite + mLinkNode.link);
		if( bbsLinkNodes==null ){
			Toast.makeText(this, "网络连接出错，请检查", Toast.LENGTH_SHORT).show();
			Log.d(TAG, "bbsLinkNodes == null");
			return;
		}
		Log.v(TAG, "" + bbsLinkNodes.size());
		LayoutParams layoutParams = new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
		for( BBSLinkNode node : bbsLinkNodes ){
			TextView tv = new TextView(this);
			tv.setLayoutParams(layoutParams);
			tv.setText(node.title);
			tv.setTag(node);
			tv.setOnClickListener( new View.OnClickListener() {
				
				@Override
				public void onClick(View v) {
					BBSLinkNode node = (BBSLinkNode)v.getTag();
					//show node decription
					Intent intent = new Intent(ShowBBSEntry.this, ShowEntryContent.class);
					intent.putExtra("linknode", node);
					startActivity(intent);
				}
			});
			mLayout.addView(tv);
		}
	}
	
	public static ArrayList<BBSLinkNode> getEntry(String link){
		ArrayList<BBSLinkNode> itemContent = new ArrayList<BBSLinkNode>(); 
		service = new HtmlServices();
		
		DocumentBuilderFactory domfac = DocumentBuilderFactory.newInstance(); 
        DocumentBuilder dombuilder = null; 
        Document doc = null; 
        
		// TODO Auto-generated method stub
		try {			
            dombuilder = domfac.newDocumentBuilder(); 	          
    		InputStream strXML = (InputStream) service.readHtml(BBSSysuSite + link);
    		doc = dombuilder.parse(strXML); 
//            try { 
//                  doc = dombuilder.parse(strXML); 
//            } catch (DOMException dom) { 
//                  System.err.println(dom.getMessage()); 
//                  System.exit(1); 
//            } catch (IOException ioe) { 
//                  System.err.println(ioe); 
//                  System.exit(1); 
//            } 
            
            //解析XML
            Element root = doc.getDocumentElement(); 
            NodeList BBSItems = root.getElementsByTagName("item"); 
            for (int i = 0; i < BBSItems.getLength(); i++) { 
                Element oneBBSItem = ( Element ) BBSItems.item(i); 
//              ItemsBean itemtBean = new ItemsBean(); 
                BBSLinkNode itemBean = new BBSLinkNode();

                NodeList titles = oneBBSItem.getElementsByTagName("title"); 
                if (titles.getLength() == 1) { 
                      Element e = (Element) titles.item(0); 
//                      Text t = (Text) e.getFirstChild(); 
                      Node t = e.getFirstChild();
                      itemBean.title = t.getNodeValue(); 
                      Log.d(TAG, itemBean.title);
                } 

                NodeList links = oneBBSItem.getElementsByTagName("link"); 
                if (links.getLength() == 1) { 
                      Element e = (Element) links.item(0); 
                      Node t = e.getFirstChild();
                      itemBean.link = t.getNodeValue(); 
                      Log.d(TAG, itemBean.link);
                } 

                NodeList desc = oneBBSItem.getElementsByTagName("description"); 
                if (desc.getLength() == 1) { 
                      Element e = (Element) desc.item(0); 
                      Node t = e.getFirstChild(); 
//                      itemtBean.description = (Integer.parseInt(t.getNodeValue())); 
                      itemBean.description =  t.getNodeValue();
                      Log.d(TAG, itemBean.description);
                }                
                itemContent.add(itemBean); 
            }
            return itemContent;
			
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (Throwable e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}
}
